package appapi.controller.oider;

import appapi.common.ResponseBean;
import appapi.entity.mqtt.Areapath;
import appapi.entity.mqtt.Devices;
import appapi.service.AreapathService;
import appapi.service.DevicesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @version: java version 1.8
 * @Author: Ygp Work
 * @description:
 * @date: 2023-07-05 10:24
 */

@Api(tags = "设备列表接口")
@RestController
@RequestMapping("/api/devices")
@CrossOrigin
public class DevicesController {
    @Autowired
    private DevicesService devicesService;

    @PostMapping
    @ApiOperation(value = "更新设备列表")
    @CrossOrigin
    public ResponseBean insertDevices(@RequestBody Devices devices) {
        Areapath areapath = new Areapath();
        devices.setNowTime(LocalDateTime.now());
        areapath.setRegionname(devices.getAddress());
        devicesService.insertDevices(devices);
        return ResponseBean.success();
    }

    @PutMapping("/{devicesId}")
    @ApiOperation(value = "修改设备列表")
    @CrossOrigin
    public ResponseBean updateDevices(@PathVariable Long devicesId, @RequestBody Devices devices) {
        Devices existingDevice = devicesService.getDevicesById(devicesId);
        if (existingDevice != null) {
            devices.setDevicesId(devicesId);
            devicesService.updateDevices(devices);
            return ResponseBean.success();
        } else {
            return ResponseBean.fail("修改失败，找不到指定的设备");
        }
    }

    @DeleteMapping("/{devicesId}")
    @ApiOperation(value = "删除设备列表")
    @CrossOrigin
    public ResponseBean deleteDevices(@PathVariable Long devicesId) {
        Devices existingDevice = devicesService.getDevicesById(devicesId);
        if (existingDevice != null) {
            devicesService.deleteDevices(devicesId);
            return ResponseBean.success();
        } else {
            return ResponseBean.fail("删除失败，找不到指定的设备");
        }
    }

    @ApiOperation(value = "根据id拿设备列表")
    @GetMapping("/{devicesId}")
    @CrossOrigin
    public ResponseBean getDevicesById(@PathVariable Long devicesId) {
        Devices devices = devicesService.getDevicesById(devicesId);
        if (devices != null) {
            return ResponseBean.success(devices);
        } else {
            return ResponseBean.fail("查询失败，找不到指定的设备");
        }
    }

    @GetMapping
    @ApiOperation(value = "查询所有的列表")
    @CrossOrigin
    public ResponseBean<List<Devices>> getAllDevices(Devices devices) {
        List<Devices> devicesList = devicesService.getAll(devices);
        int size = devicesList.size();
        if (!devicesList.isEmpty()) {
            return ResponseBean.success(size, devicesList);
        } else {
            return ResponseBean.fail("查询失败，设备列表为空");
        }
    }

    @ApiOperation(value = "删除地区下的具体设备")
    @PostMapping("/deleteDeviceByRegion")
    public ResponseBean deleteDeviceByRegion(@RequestBody Devices devices) {
        try {
            devicesService.deleteDeviceByRegion(devices.getDevicesId(), devices.getAddress());
            return ResponseBean.success("删除成功");
        } catch (Exception e) {
            return ResponseBean.fail("删除失败");
        }
    }
}
